package com.china08.yxyapi.service.manage.tec.teaching.homework.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workcorrect.WorkcorrectFindClassStuTaskController.ClassStuTaskReport;
import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workcorrect.WorkcorrectFindClassStuTaskController.RespModel;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskClass;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkCorrectService;

@Service
public class HomeworkCorrectServiceImpl implements HomeworkCorrectService {

    @Override
    public RespModel convertRespModel(HomeworkTaskClass homeworkTaskClass, List<HomeworkTaskReport> homeworkTaskReports,
            List<HomeworkTaskReportQues> homeworkTaskReportQueslist) {
        // return
        RespModel respModel = new RespModel();
        List<ClassStuTaskReport> classStuTaskReports = new ArrayList<ClassStuTaskReport>();
        // 用户答题信息放入 Map<String, String>
        Map<String, String> stuAnswerTexts = new HashMap<String, String>();
        Map<String, Integer> stuMarks = new HashMap<String, Integer>();
        Map<String, String> stuTaskReportQuesIdks = new HashMap<String, String>();
        homeworkTaskReportQueslist.forEach(homeworkTaskReportQues -> {
            stuAnswerTexts.put(homeworkTaskReportQues.getStudentId(), homeworkTaskReportQues.getAnswerText());
            stuMarks.put(homeworkTaskReportQues.getStudentId(), homeworkTaskReportQues.getNotifyPassed());
            stuTaskReportQuesIdks.put(homeworkTaskReportQues.getStudentId(), homeworkTaskReportQues.getId());
        });
        // ##
        respModel.taskId = homeworkTaskClass == null ? "" : homeworkTaskClass.getTaskId();
        respModel.taskName = homeworkTaskClass == null ? "" : homeworkTaskClass.getTitle();
        respModel.grade = "";
        respModel.className = homeworkTaskClass == null ? "" : homeworkTaskClass.getClassName();
        // 手工排序，按照学生完成时间排序，-1-逾期未提交,0-未做,3-答题中 排在后面，1-完成 ,2-已批阅排在前面
        List<HomeworkTaskReport> list1 = new ArrayList<>(); // 1-完成 ,2-已批阅
        List<HomeworkTaskReport> list2 = new ArrayList<>(); // 3-答题中
        List<HomeworkTaskReport> list3 = new ArrayList<>(); // -1-逾期未提交,0-未做
        homeworkTaskReports.forEach(homeworkTaskReport -> {
            if (homeworkTaskReport.getStatusOfFinished() == 1 || homeworkTaskReport.getStatusOfFinished() == 2) {
                list1.add(homeworkTaskReport);
            } else if (homeworkTaskReport.getStatusOfFinished() == 3) {
                list2.add(homeworkTaskReport);
            } else if (homeworkTaskReport.getStatusOfFinished() == -1
                    || homeworkTaskReport.getStatusOfFinished() == 0) {
                list3.add(homeworkTaskReport);
            }
        });
        // return
        list1.forEach(homeworkTaskReport -> {
            ClassStuTaskReport classStuTaskReport = new ClassStuTaskReport();
            classStuTaskReport.taskId = homeworkTaskReport.getTaskId();
            classStuTaskReport.classId = homeworkTaskReport.getClassId();
            classStuTaskReport.className = homeworkTaskReport.getClassName();
            classStuTaskReport.studentId = homeworkTaskReport.getStudentId();
            classStuTaskReport.studentNick = homeworkTaskReport.getStudentNick();
            classStuTaskReport.studentImg = homeworkTaskReport.getAvatar();
            classStuTaskReport.mark = stuMarks.get(homeworkTaskReport.getStudentId());
            classStuTaskReport.statusOfFinished = homeworkTaskReport.getStatusOfFinished();
            classStuTaskReport.answerText = stuAnswerTexts.get(homeworkTaskReport.getStudentId());
            classStuTaskReport.taskReportQuesId = stuTaskReportQuesIdks.get(homeworkTaskReport.getStudentId());
            classStuTaskReports.add(classStuTaskReport);
        });
        list2.forEach(homeworkTaskReport -> {
            ClassStuTaskReport classStuTaskReport = new ClassStuTaskReport();
            classStuTaskReport.taskId = homeworkTaskReport.getTaskId();
            classStuTaskReport.classId = homeworkTaskReport.getClassId();
            classStuTaskReport.className = homeworkTaskReport.getClassName();
            classStuTaskReport.studentId = homeworkTaskReport.getStudentId();
            classStuTaskReport.studentNick = homeworkTaskReport.getStudentNick();
            classStuTaskReport.studentImg = homeworkTaskReport.getAvatar();
            classStuTaskReport.mark = stuMarks.get(homeworkTaskReport.getStudentId());
            classStuTaskReport.statusOfFinished = homeworkTaskReport.getStatusOfFinished();
            classStuTaskReport.answerText = stuAnswerTexts.get(homeworkTaskReport.getStudentId());
            classStuTaskReport.taskReportQuesId = stuTaskReportQuesIdks.get(homeworkTaskReport.getStudentId());
            classStuTaskReports.add(classStuTaskReport);
        });
        list3.forEach(homeworkTaskReport -> {
            ClassStuTaskReport classStuTaskReport = new ClassStuTaskReport();
            classStuTaskReport.taskId = homeworkTaskReport.getTaskId();
            classStuTaskReport.classId = homeworkTaskReport.getClassId();
            classStuTaskReport.className = homeworkTaskReport.getClassName();
            classStuTaskReport.studentId = homeworkTaskReport.getStudentId();
            classStuTaskReport.studentNick = homeworkTaskReport.getStudentNick();
            classStuTaskReport.studentImg = homeworkTaskReport.getAvatar();
            classStuTaskReport.mark = stuMarks.get(homeworkTaskReport.getStudentId());
            classStuTaskReport.statusOfFinished = homeworkTaskReport.getStatusOfFinished();
            classStuTaskReport.answerText = stuAnswerTexts.get(homeworkTaskReport.getStudentId());
            classStuTaskReport.taskReportQuesId = stuTaskReportQuesIdks.get(homeworkTaskReport.getStudentId());
            classStuTaskReports.add(classStuTaskReport);
        });
        respModel.classStuTaskReports = classStuTaskReports;
        return respModel;
    }

}
